﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Web.Modules.Shared.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UserMessageSenderControl : BaseUserControl<UserMessageSenderControlViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {

            }
        }

        /// <summary>
        /// Handles the refreshData event of the store control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.StoreRefreshDataEventArgs"/> instance containing the event data.</param>
        protected void store_refreshData(object sender, StoreRefreshDataEventArgs args)
        {
            string filter = args.Parameters["query"] ?? "";
            Paging<IUser> users = this.IUsersPaging(args.Start, args.Limit, args.Sort, args.Dir.ToString(), filter);

            this.storeUsers.DataSource = users.Data;
            this.storeUsers.DataBind();
        }

        /// <summary>
        /// Is the users paging.
        /// </summary>
        /// <param name="start">The start.</param>
        /// <param name="limit">The limit.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="dir">The dir.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        private Paging<IUser> IUsersPaging(int start, int limit, string sort, string dir, string filter)
        {
            List<IUser> users = this.ViewModel.GetAllUsers(RegistryInfo.CommitteeID);
            if (!string.IsNullOrEmpty(filter) && filter != "*")
            {
                users.RemoveAll(user => !user.DisplayName.ToLower().Contains(filter.ToLower()));
            }

            if (!string.IsNullOrEmpty(sort))
            {
                users.Sort(delegate(IUser x, IUser y)
                {
                    object a;
                    object b;

                    int direction = dir == "DESC" ? -1 : 1;

                    a = x.GetType().GetProperty(sort).GetValue(x, null);
                    b = y.GetType().GetProperty(sort).GetValue(y, null);

                    return CaseInsensitiveComparer.Default.Compare(a, b) * direction;
                });
            }

            if ((start + limit) > users.Count)
            {
                limit = users.Count - start;
            }

            List<IUser> rangeIUsers = (start < 0 || limit < 0) ? users : users.GetRange(start, limit);

            return new Paging<IUser>(rangeIUsers, users.Count);
        }

        /// <summary>
        /// Handles the Clicked event of the btnSend control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnSend_Clicked(object sender, AjaxEventArgs args)
        {
            var To = comboNameSearch.SelectedItem.Value.ToGuid(); ;
            var message = txtMessage.Text;
            var subject = txtSubject.Text;

            this.ViewModel.SendMessage(To, subject, message);

            if (SendButtonClicked != null)
                SendButtonClicked(sender, args);

        }

        /// <summary>
        /// Cleans the control.
        /// </summary>
        public void CleanControl()
        {
            txtMessage.Text = string.Empty;
            txtSubject.Text = string.Empty;
            comboNameSearch.ClearValue();
        }

        /// <summary>
        /// Occurs when [send button clicked].
        /// </summary>
        public event EventHandler<AjaxEventArgs> SendButtonClicked;
    }
}